#!/bin/bash
# 部署脚本 - 包含微信小程序配置的 HTTPS 服务

set -e  # 遇到错误立即退出

echo "========================================"
echo "部署 flooddataaugmentation (HTTPS + 微信)"
echo "========================================"

# 配置信息
CONTAINER_NAME="flooddataaugmentation_ssl"
IMAGE_NAME="flooddataaugmentation:uv"
PORT=8090

# 微信小程序配置（从环境变量或配置文件读取）
WECHAT_APPID="${WECHAT_APPID:-wx243c0fdef45edc64}"
WECHAT_SECRET="${WECHAT_SECRET:-}"

# 检查配置
if [ -z "$WECHAT_SECRET" ]; then
    echo "❌ 错误: 未设置 WECHAT_SECRET 环境变量"
    echo ""
    echo "请先设置环境变量："
    echo "  export WECHAT_SECRET=\"你的AppSecret\""
    echo ""
    echo "或者使用 .env 文件："
    echo "  cp docker_file/.env.example docker_file/.env"
    echo "  # 编辑 .env 文件填入配置"
    echo "  source docker_file/.env"
    exit 1
fi

# 停止并删除旧容器
echo ""
echo "1. 清理旧容器..."
if docker ps -a | grep -q $CONTAINER_NAME; then
    docker stop $CONTAINER_NAME 2>/dev/null || true
    docker rm $CONTAINER_NAME 2>/dev/null || true
    echo "   ✅ 旧容器已删除"
else
    echo "   ℹ️  没有旧容器需要清理"
fi

# 检查镜像
echo ""
echo "2. 检查镜像..."
if ! docker images | grep -q "$IMAGE_NAME"; then
    echo "   ⚠️  镜像不存在，开始构建..."
    docker build -f docker_file/Dockerfile -t $IMAGE_NAME .
    echo "   ✅ 镜像构建完成"
else
    echo "   ✅ 镜像已存在: $IMAGE_NAME"
fi

# 检查 SSL 证书
echo ""
echo "3. 检查 SSL 证书..."
if [ ! -f "/etc/letsencrypt/live/ws.waterism.tech/fullchain.pem" ]; then
    echo "   ❌ 错误: SSL 证书不存在"
    echo "   证书路径: /etc/letsencrypt/live/ws.waterism.tech/"
    exit 1
fi
echo "   ✅ SSL 证书存在"

# 启动新容器
echo ""
echo "4. 启动新容器..."
docker run --name $CONTAINER_NAME -d \
  --network=host \
  --restart=unless-stopped \
  -v ~/minio:/app/minio:rw \
  -v /etc/letsencrypt:/etc/letsencrypt:ro \
  -e WECHAT_APPID="$WECHAT_APPID" \
  -e WECHAT_SECRET="$WECHAT_SECRET" \
  $IMAGE_NAME \
  uv run python /app/backend/run_server.py \
    --host 0.0.0.0 \
    --port $PORT \
    --ssl \
    --ssl-certfile /etc/letsencrypt/live/ws.waterism.tech/fullchain.pem \
    --ssl-keyfile /etc/letsencrypt/live/ws.waterism.tech/privkey.pem

echo "   ✅ 容器已启动"

# 等待服务启动
echo ""
echo "5. 等待服务启动..."
sleep 3

# 检查容器状态
if docker ps | grep -q $CONTAINER_NAME; then
    echo "   ✅ 容器运行正常"
else
    echo "   ❌ 容器启动失败"
    echo ""
    echo "查看日志："
    docker logs $CONTAINER_NAME
    exit 1
fi

# 显示日志
echo ""
echo "6. 服务日志（最近20行）："
echo "----------------------------------------"
docker logs --tail 20 $CONTAINER_NAME

# 测试服务
echo ""
echo "7. 测试服务..."
echo "   测试 HTTPS 访问..."
if curl -k -s https://127.0.0.1:$PORT/ > /dev/null 2>&1; then
    echo "   ✅ HTTPS 服务正常"
else
    echo "   ⚠️  HTTPS 服务可能未就绪，请稍后测试"
fi

echo ""
echo "   测试微信配置..."
if curl -k -s https://127.0.0.1:$PORT/api/wechat/check-config | grep -q "configured"; then
    echo "   ✅ 微信配置已加载"
else
    echo "   ⚠️  微信配置可能未加载，请检查日志"
fi

# 完成
echo ""
echo "========================================"
echo "✅ 部署完成！"
echo "========================================"
echo ""
echo "服务信息："
echo "  - 容器名称: $CONTAINER_NAME"
echo "  - HTTPS 端口: $PORT"
echo "  - API 文档: https://59.46.146.11:$PORT/docs"
echo "  - 微信 AppID: $WECHAT_APPID"
echo ""
echo "常用命令："
echo "  查看日志: docker logs -f $CONTAINER_NAME"
echo "  停止服务: docker stop $CONTAINER_NAME"
echo "  重启服务: docker restart $CONTAINER_NAME"
echo ""
echo "测试命令："
echo "  curl -k https://127.0.0.1:$PORT/"
echo "  curl -k https://127.0.0.1:$PORT/api/wechat/check-config"
echo ""

